<?php

namespace App\Library\Logic;

use App\Models\SmsModel;
use Illuminate\Database\Eloquent\Model;

class SmsLogic extends SmsModel
{
    /**
     * frequently
     * @param array $data
     * @param int $limit
     * @return bool
     */
    public static function frequently(array $data, int $limit): bool
    {
        $createdAt = self::valueByWhere(array_merge($data, [
            'isUse' => 0, 'status' => 1, 'orderByDesc' => 'createdAt'
        ]), null, 'createdAt');
        if (is_null($createdAt)) return false;
        else if (now()->parse($createdAt)->addSeconds($limit)->gte(now())) return true;
        else return false;
    }

    /**
     * noUse
     * @param array $data
     * @return Model|null
     */
    public static function noUse(array $data): ?Model
    {
        return SmsLogic::findByWhere(array_merge($data, [
            'isUse' => 0, 'status' => 1, 'ip' => request()->ip()
        ]), null, 'id');
    }
}
